﻿Ext.BLANK_IMAGE_URL = basePath + 'ext/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = basePath + 'ext/resources/images/default/s.gif';

var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.system.RoleManage';
var status;
var nodes = new Array();

Ext.onReady(function() {
	Ext.QuickTips.init();

	/**
	 * 用户树
	 */
	var leftTree = new Ext.tree.TreePanel({
				id : 'leftTree',
				animate : true,
				border : false,
				buttonAlign : 'right',
				region : 'west',
				height : '100%',
				rootVisible : false,
				loader : new Ext.tree.TreeLoader({
							dataUrl : clsUrl
									+ '&methodName=getUserTree&paramCount=0'
						}),
				root : new Ext.tree.AsyncTreeNode({
							text : '项目',
							id : '-1',
							groups : '-1'
						})
			});

	/**
	 * 菜单树
	 */
	var rightTree = new Ext.tree.TreePanel({
				id : 'rightTree',
				animate : true,
				border : false,
				region : 'center',
				buttonAlign : 'right',
				height : '100%',
				rootVisible : true,
				loader : new Ext.tree.TreeLoader({
							dataUrl : ''
						}),
				root : new Ext.tree.AsyncTreeNode({
							text : '功能菜单',
							id : '-1'
						})
			});

	rightTree.on("beforeexpandnode", function(node) {
				bodyPanel.body.mask('树节点正在展开，请稍后操作', 'x-mask-loading');
				nodes.push(node.id);
			});

	rightTree.on("expandnode", function(node) {
				nodes.remove(node.id);
				if (nodes.length == 0) {
					bodyPanel.body.unmask();
				}
			});

	/**
	 * 用户树Panel
	 */
	var leftPanel = new Ext.Panel({
				id : 'leftPanel',
				border : true,
				region : 'west',
				width : 200,
				split : true,
				collapsible : false,
				items : [leftTree]
			});

	var mainPanel = new Ext.Panel({
				id : 'mainPanel',
				border : true,
				bodyStyle : 'background-color:#ffffff',
				layout : 'border',
				region : 'center',
				split : true,
				tbar : [{
							text : '保存授权',
							id : 'savePurview',
							iconCls : 'save',
							handler : doSave
						}],
				items : [rightTree]
			});

	var bodyPanel = new Ext.Panel({
				id : 'bodyPanel',
				border : false,
				layout : 'border',
				region : 'center',
				items : [leftPanel, mainPanel]
			});

	var viewport = new Ext.Viewport({
				layout : 'border',
				items : [bodyPanel]
			});

	rightTree.hide();

	/**
	 * 用户树节点事件
	 */
	leftTree.on("click", function(node) {
		if (node.id == -1) {
			rightTree.hide();
			Ext.getCmp("savePurview").disable();
		} else {
			rightTree.show();
			nodes = new Array();
			Ext.getCmp("savePurview").enable();
			var pid = node.parentNode.id;
			rightTree.root.setText("【" + node.parentNode.text + "】功能菜单")
			rightTree.loader.dataUrl = clsUrl
					+ '&methodName=getMenuTree&paramCount=3&param_0=-1&param_1='
					+ node.id + '&param_2=2'
			rightTree.root.reload();
			rightTree.expandAll();
		}
	});

	Ext.getCmp("savePurview").disable();
	function doSave() {
		var record = leftTree.getSelectionModel().getSelectedNode();

		if (record) {
			Ext.Msg.show({
						title : '提示',
						width : 200,
						msg : '是否进行授权保存？',
						buttons : Ext.Msg.YESNO,
						fn : function(btn) {
							if ("yes" == btn) {
								saveMenu();
							}
						},
						icon : Ext.MessageBox.QUESTION
					});
		}

		function saveMenu() {
			var record = rightTree.getSelectionModel().getSelectedNode();
			var obj = new Object();
			obj.roleid = leftTree.getSelectionModel().getSelectedNode().id;
			var records = [];
			var list = rightTree.getChecked();
			obj.menuid = "";
			for (i = 0; i < list.length; i++) {
				var node = list[i];
				var id = node.id;
				records[i] = id;
				obj.menuid += records[i] + ",";
			}
			var url = clsUrl + '&methodName=insertMenu&paramCount=0'
			httpReq = null;
			httpReq = getXMLHttpRequest();
			httpReq.onreadystatechange = onPurviewReturn;
			httpReq.open("POST", url, true);
			httpReq.send(Ext.util.JSON.encode(obj));
		}
	}

	function onPurviewReturn() {
		if (httpReq.readyState == 4) {
			if (httpReq.status == 200) {
				if (httpReq.responseText == "success") {
					Ext.Msg.show({
								title : '成功提示',
								width : 200,
								msg : '保存成功！',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.INFO
							});
					rightTree.root.reload();
					rightTree.expandAll();
					httpReq = null;
				} else {
					Ext.Msg.show({
								title : '错误提示',
								width : 200,
								msg : httpReq.responseText,
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR
							});
				}
			}
		}
	}

});
